package inits

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"log"
	"user_service/basic/config"
	"user_service/handler/model"
)

func InitMysql() {
	var err error
	mysqlCong := config.AppCong.Mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		mysqlCong.User, mysqlCong.Password, mysqlCong.Host, mysqlCong.Port, mysqlCong.Database)
	config.DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		log.Fatal("数据库链接失败", err)
	}
	log.Println("mysql init success")

	//数据表迁移
	if err := config.DB.AutoMigrate(
		&model.User{},
		&model.Good{},
		&model.Order{},
		&model.Like{},
	); err != nil {
		panic(err)
	}
	log.Println("数据表迁移成功")
}
